package limeirafol.gui.Cadastro;

import java.awt.event.KeyEvent;
import java.util.Vector;
import javax.swing.SwingConstants;
import limeirafol.bean.BeanCadastro;
import limeirafol.remote.cadastro.MunicipioFiltroConsultaVO;
import limeirafol.remote.cadastro.MunicipioVO;
import lbframework.bean.internalFrame.LBInternalFrame;
import lbframework.bean.mdiFrame.LBMdiFrame;
import lbframework.bean.table.LBColumnTable;
import lbframework.classe.Util;
import lbframework.classe.LBException;
import lbframework.remote.ItemComboVO;

public class MunicipioConsultaGUI extends LBInternalFrame {

    private Vector<MunicipioVO> vMunicipio = null;
    private BeanCadastro beanCadastro = new BeanCadastro();

    public MunicipioConsultaGUI(LBMdiFrame i_mdiFrame) throws Exception {
        super(i_mdiFrame);

        initComponents();

        centralizarForm();
        this.setMaximum(true);
        cboEstado.setTabela("estado");
        cboEstado.setCampoDescricao("sigla");
        cboEstado.carregar();
        cboEstado.addItem(new ItemComboVO(-1, "TODOS"));

        cboEstado.setId(-1);


    }

    @Override
    public void consultar() throws Exception {
        MunicipioFiltroConsultaVO oFiltro = new MunicipioFiltroConsultaVO();

        if (!txtNome.getText().equals("")) {
            oFiltro.descricao = txtNome.getText();
        }

        oFiltro.idEstado = cboEstado.getId();

        vMunicipio = beanCadastro.consultarMunicipio(oFiltro);

        exibirConsulta();
    }

    private void exibirConsulta() throws Exception {
        LBColumnTable[] colunas = new LBColumnTable[]{
            new LBColumnTable("Código", 70, true, SwingConstants.LEFT, false, null),
            new LBColumnTable("Nome", 300, true, SwingConstants.LEFT, false, null),
            new LBColumnTable("Estado", 60, true, SwingConstants.LEFT, false, null),
            new LBColumnTable("DIPAM", 70, true, SwingConstants.LEFT, false, null),
            new LBColumnTable("DIPJ", 70, true, SwingConstants.LEFT, false, null)
        };

        Object[][] dados = new Object[vMunicipio.size()][colunas.length];

        int i = 0;

        for (MunicipioVO oMunicipio : vMunicipio) {
            dados[i][0] = String.valueOf(oMunicipio.id);
            dados[i][1] = oMunicipio.descricao;
            dados[i][2] = oMunicipio.estado;

            i++;
        }

        tblConsulta.setModel(dados, colunas);

        tblConsulta.requestFocus();
    }

    private void carregarMunicipio() throws Exception {
        if (tblConsulta.getLinhaSelecionada() == -1) {
            throw new LBException(Util.MSG_NENHUM_ITEM_SELECIONADO);
        }

        MunicipioVO oMunicipio = vMunicipio.get(tblConsulta.getLinhaSelecionada());

        if (consultaCampo != null) {
            this.setVisible(false);

            consultaCampo.setParentSelected(true);
            consultaCampo.setId(oMunicipio.id);
            consultaCampo.consultar(true);
            consultaCampo.transferFocus();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        vRPanel1 = new lbframework.bean.panel.LBPanel();
        vRLabel2 = new lbframework.bean.label.LBLabel();
        txtNome = new lbframework.bean.textField.LBTextField();
        cboEstado = new lbframework.bean.comboBox.LBComboBox();
        vRLabel4 = new lbframework.bean.label.LBLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tblConsulta = new lbframework.bean.table.LBTable();
        vRPanel2 = new lbframework.bean.panel.LBPanel();
        btnConsultar = new lbframework.bean.button.LBButton();
        bntSair = new lbframework.bean.button.LBButton();
        vRToolBarPadrao1 = new lbframework.bean.toolBarPadrao.LBToolBarPadrao(this);

        setClosable(true);
        setTitle("Consulta de Município");

        vRLabel2.setText("Nome");

        txtNome.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtNomeActionPerformed(evt);
            }
        });

        cboEstado.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cboEstadoActionPerformed(evt);
            }
        });

        vRLabel4.setText("Estado");

        javax.swing.GroupLayout vRPanel1Layout = new javax.swing.GroupLayout(vRPanel1);
        vRPanel1.setLayout(vRPanel1Layout);
        vRPanel1Layout.setHorizontalGroup(
            vRPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(vRPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(vRPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(vRLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtNome, javax.swing.GroupLayout.PREFERRED_SIZE, 296, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(vRPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(vRLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(cboEstado, javax.swing.GroupLayout.PREFERRED_SIZE, 73, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );
        vRPanel1Layout.setVerticalGroup(
            vRPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(vRPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(vRPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(vRPanel1Layout.createSequentialGroup()
                        .addComponent(vRLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(cboEstado, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(vRPanel1Layout.createSequentialGroup()
                        .addComponent(vRLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txtNome, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(13, Short.MAX_VALUE))
        );

        tblConsulta.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {

            }
        ));
        tblConsulta.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tblConsultaMouseClicked(evt);
            }
        });
        tblConsulta.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                tblConsultaFocusGained(evt);
            }
            public void focusLost(java.awt.event.FocusEvent evt) {
                tblConsultaFocusLost(evt);
            }
        });
        tblConsulta.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                tblConsultaKeyPressed(evt);
            }
        });
        jScrollPane1.setViewportView(tblConsulta);

        btnConsultar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/lbframework/img/consultar.png"))); // NOI18N
        btnConsultar.setText("Consultar");
        btnConsultar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnConsultarActionPerformed(evt);
            }
        });

        bntSair.setIcon(new javax.swing.ImageIcon(getClass().getResource("/lbframework/img/sair.png"))); // NOI18N
        bntSair.setText("Sair");
        bntSair.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bntSairActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout vRPanel2Layout = new javax.swing.GroupLayout(vRPanel2);
        vRPanel2.setLayout(vRPanel2Layout);
        vRPanel2Layout.setHorizontalGroup(
            vRPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, vRPanel2Layout.createSequentialGroup()
                .addContainerGap(408, Short.MAX_VALUE)
                .addComponent(btnConsultar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(8, 8, 8)
                .addComponent(bntSair, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        vRPanel2Layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {bntSair, btnConsultar});

        vRPanel2Layout.setVerticalGroup(
            vRPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(vRPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(btnConsultar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addComponent(bntSair, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        vRToolBarPadrao1.setRollover(true);
        vRToolBarPadrao1.setVisibleConsultar(true);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(vRPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jScrollPane1, 0, 0, Short.MAX_VALUE)
                    .addComponent(vRPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, 626, Short.MAX_VALUE)
                    .addComponent(vRToolBarPadrao1, javax.swing.GroupLayout.DEFAULT_SIZE, 626, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(vRToolBarPadrao1, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(vRPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 202, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(vRPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents
    private void txtNomeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtNomeActionPerformed
    }//GEN-LAST:event_txtNomeActionPerformed

    private void cboEstadoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cboEstadoActionPerformed
}//GEN-LAST:event_cboEstadoActionPerformed

    private void btnConsultarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnConsultarActionPerformed
        try {
            consultar();

        } catch (Exception ex) {
            Util.exibirMensagemErro(ex, getTitle());
        }
    }//GEN-LAST:event_btnConsultarActionPerformed

    private void bntSairActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bntSairActionPerformed
        try {
            sair();

        } catch (Exception ex) {
            Util.exibirMensagemErro(ex, getTitle());
        }
    }//GEN-LAST:event_bntSairActionPerformed

    private void tblConsultaMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tblConsultaMouseClicked
        try {
            if (evt.getClickCount() == 2) {
                carregarMunicipio();
            }

        } catch (Exception ex) {
            Util.exibirMensagemErro(ex, getTitle());
        }
    }//GEN-LAST:event_tblConsultaMouseClicked

    private void tblConsultaKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_tblConsultaKeyPressed

        try {
            if (evt.getKeyCode() == KeyEvent.VK_ENTER) {
                carregarMunicipio();
            }

        } catch (Exception ex) {
            Util.exibirMensagemErro(ex, getTitle());
        }
    }//GEN-LAST:event_tblConsultaKeyPressed

    private void tblConsultaFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_tblConsultaFocusGained
    }//GEN-LAST:event_tblConsultaFocusGained

    private void tblConsultaFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_tblConsultaFocusLost
    }//GEN-LAST:event_tblConsultaFocusLost
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private lbframework.bean.button.LBButton bntSair;
    private lbframework.bean.button.LBButton btnConsultar;
    private lbframework.bean.comboBox.LBComboBox cboEstado;
    private javax.swing.JScrollPane jScrollPane1;
    private lbframework.bean.table.LBTable tblConsulta;
    private lbframework.bean.textField.LBTextField txtNome;
    private lbframework.bean.label.LBLabel vRLabel2;
    private lbframework.bean.label.LBLabel vRLabel4;
    private lbframework.bean.panel.LBPanel vRPanel1;
    private lbframework.bean.panel.LBPanel vRPanel2;
    private lbframework.bean.toolBarPadrao.LBToolBarPadrao vRToolBarPadrao1;
    // End of variables declaration//GEN-END:variables
}
